/* === Chat Widget === */

.chat-modal {
    width: 880px;
    max-width: 92vw;
    max-height: 88vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-sizing: border-box;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px 16px;
    min-height: 0;
}

.chat-empty {
    text-align: center;
    color: var(--text-muted);
    padding: 40px 16px;
    font-size: 0.95rem;
}

/* --- Messages (grid layout) --- */

.chat-msg {
    display: grid;
    grid-template-columns: 32px 1fr;
    grid-template-rows: auto 1fr;
    column-gap: 8px;
    row-gap: 0;
    margin-bottom: 10px;
    align-items: start;
}

.chat-msg-avatar-wrap {
    grid-row: 1 / 3;
    grid-column: 1;
}

.chat-msg-body {
    grid-column: 2;
}

.chat-msg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    border: 1px solid var(--border);
    display: block;
}

/* Header: name + time + reply btn */

.chat-msg-header {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 3px;
}

.chat-msg-name {
    font-weight: 600;
    font-size: 0.85rem;
    color: var(--text);
    text-decoration: none;
}

.chat-msg-name:hover {
    text-decoration: underline;
}

.chat-msg-time {
    font-size: 0.7rem;
    color: var(--text-muted);
}

.chat-msg-text {
    font-size: 0.9rem;
    line-height: 1.45;
    color: var(--text);
    word-break: break-word;
    white-space: pre-wrap;
}

/* Reply button */

.chat-reply-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.8rem;
    padding: 2px 4px;
    border-radius: 3px;
    margin-left: auto;
    transition: color 0.15s, background 0.15s;
}

.chat-reply-btn:hover {
    background: var(--bg);
    color: var(--accent);
}

/* Reply chain (quoted parent message) */

.chat-msg-reply-to {
    margin-left: 0;
    padding: 4px 10px;
    margin-bottom: 4px;
    background: var(--bg);
    border-left: 3px solid var(--accent);
    border-radius: 0 6px 6px 0;
    font-size: 0.8rem;
    color: var(--text-secondary);
    cursor: pointer;
    transition: background 0.15s;
}

.chat-msg-reply-to:hover {
    background: var(--border);
}

.chat-msg-reply-to-name {
    font-weight: 600;
    color: var(--text);
}

.chat-msg-reply-to-text {
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
    max-width: 100%;
    white-space: nowrap;
}

/* Reply preview bar above input */

.chat-reply-preview {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    background: var(--bg);
    border-left: 3px solid var(--accent);
    font-size: 0.82rem;
    color: var(--text-secondary);
}

.chat-reply-text {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-reply-cancel {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1rem;
    padding: 0 2px;
    line-height: 1;
}

.chat-reply-cancel:hover {
    color: var(--text);
}

/* --- Input area (inline-block, not flex) --- */

.chat-input-area {
    border-top: 1px solid var(--border);
    padding: 6px 0;
}

#chat-input {
    display: block;
    margin: 0 auto;
    width: 500px;
    resize: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 0.9rem;
    font-family: inherit;
    background: var(--bg-card);
    color: var(--text);
    max-height: 120px;
    min-height: 36px;
    line-height: 1.4;
    box-sizing: border-box;
}

#chat-input:focus {
    outline: none;
    border-color: var(--accent);
}

.chat-send-btn {
    display: block;
    margin: 6px auto 0;
    width: 500px;
    padding: 8px;
    border-radius: 8px;
    font-size: 0.9rem;
    text-align: center;
    cursor: pointer;
}

/* --- Scrollbar --- */

.chat-messages::-webkit-scrollbar {
    width: 4px;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

/* --- Nav button --- */

.nav-chat-btn {
    /* visible in both desktop top nav and mobile bottom nav */
}

/* --- Sidebar chat preview --- */

.sidebar-chat-preview {
    background: var(--bg);
    border-radius: 12px;
    padding: 12px;
}

.chat-preview-messages {
    max-height: 280px;
    overflow-y: auto;
}

.chat-preview-msg {
    display: flex;
    gap: 8px;
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
    align-items: flex-start;
}

.chat-preview-msg:last-child {
    border-bottom: none;
}

.chat-preview-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    flex-shrink: 0;
    object-fit: cover;
}

.chat-preview-body {
    flex: 1;
    display: flex;
    align-items: baseline;
    gap: 6px;
    min-width: 0;
}

.chat-preview-name {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text);
    text-decoration: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-preview-name:hover {
    color: var(--accent);
}

.chat-preview-time {
    font-size: 0.7rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

.chat-preview-text {
    font-size: 0.8rem;
    color: var(--text);
    line-height: 1.3;
    word-break: break-word;
    margin-top: 2px;
}

.chat-preview-empty {
    text-align: center;
    color: var(--text-muted);
    font-size: 0.85rem;
    padding: 20px 8px;
}

.chat-preview-enter-btn {
    width: 100%;
    margin-top: 10px;
}

/* Hide preview on mobile */
@media (max-width: 768px) {
    .sidebar-chat-preview {
        display: none;
    }

    .chat-modal {
        width: calc(100vw - 10px);
        max-width: calc(100vw - 10px);
    }

    #chat-input {
        width: 250px;
    }

    .chat-send-btn {
        width: 250px;
    }
}
